package com.jlu.springbootjwt.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {

  public static String sign = "qwertyu";


  /*
  生成token
   */
  public static String getToken(Map<String,String> map){

    Calendar instance = Calendar.getInstance();

    instance.add(Calendar.DATE,7);

//    创建JWT Builder
    JWTCreator.Builder builder = JWT.create();

//    payload
    map.forEach((k,v)->{
      builder.withClaim(k,v);
    });

//    指定过期时间和加密算法
    String token = builder.withExpiresAt(instance.getTime())
            .sign(Algorithm.HMAC256(sign));

    return token;
  }

  /*
  验证token
   */
  public static DecodedJWT verrify(String token){

    return JWT.require(Algorithm.HMAC256(sign)).build().verify(token);

//    Verification verification = JWT.require(Algorithm.HMAC256(sign));
//    verification.build().verify(token);
  }

  /*
  获取token中的信息
   */
//  public static DecodedJWT getTokenInfo(String token){
//
//    DecodedJWT verify = JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
//
//    return verify;
//  }
}
